package com.shoesdemo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.shoesdemo.BaseActivity;
import com.shoesdemo.R;
import com.shoesdemo.data.TomorrowData;
import com.shoesdemo.utils.ClickUtil;
import com.shoesdemo.utils.VibratorUtils;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TextTomorrowActivity extends BaseActivity {

    private CountDownTimer mCountDownTimer;
    private static int WORKMINUTE = 25;
    private static int RELEASEMINUTE = 05;
    private static final int SECOND = 60;
    private static int TOMORROWTIME = WORKMINUTE * 60 * 1000;
    private static int RELEASETIME = RELEASEMINUTE * 60 * 1000;
    private long lastMinute = WORKMINUTE;
    private boolean isRelease;
    private TextView mTvTime;
    private TextView mTvMin, mTvSec;

    public static void start(Context context, int workTime, int releaseTime) {
        WORKMINUTE = workTime;
        RELEASEMINUTE = releaseTime;
        TOMORROWTIME = WORKMINUTE * 60 * 1000;
        RELEASETIME = RELEASEMINUTE * 60 * 1000;
        Intent intent = new Intent(context, TextTomorrowActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        actionBar.hide();
        mTvMin = findViewById(R.id.tv_minute);
        mTvSec = findViewById(R.id.tv_second);
        mTvTime = findViewById(R.id.tv_time);
        mTvTime.setText("集中时间");
    }

    @Override
    protected void initListener() {

    }

    @Override
    public int getLayoutView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_text_tomorrow;
    }

    @Override
    protected void initData() {
        startTimer(TOMORROWTIME);
        mCountDownTimer.start();
    }

    private void startTimer(int allTime) {
        mCountDownTimer = new CountDownTimer(allTime, 1000) {
            @Override
            public void onTick(long lastTimeStamp) {
                long minute = lastTimeStamp / 1000 / 60;
                long second = (lastTimeStamp - minute * 1000 * 60) / 1000;
                if (lastMinute != minute) {
                    setMinText((int) minute);
                    lastMinute = minute;
                }
                if (second == 0) {
                    second = 60;
                }
                setSecText((int) second);
            }

            @Override
            public void onFinish() {
                isRelease = !isRelease;
                if (isRelease) {
                    lastMinute = RELEASEMINUTE;
                    setMinText(RELEASEMINUTE);
                    setSecText(SECOND);
                    startTimer(RELEASETIME);
                    mCountDownTimer.start();
                    mTvTime.setText("休息时间");
                    VibratorUtils.startMusic(TextTomorrowActivity.this);
                } else {
                    lastMinute = WORKMINUTE;
                    setMinText(WORKMINUTE);
                    setSecText(SECOND);
                    startTimer(TOMORROWTIME);
                    mCountDownTimer.start();
                    mTvTime.setText("集中时间");
                    saveOneTomorrow();
                    VibratorUtils.startMusic(TextTomorrowActivity.this);
                }
            }
        };
    }

    //保存一个番茄数据
    private void saveOneTomorrow() {
        long timeMillis = System.currentTimeMillis();
        SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
        SimpleDateFormat mm = new SimpleDateFormat("MM");
        SimpleDateFormat dd = new SimpleDateFormat("dd");
        SimpleDateFormat ymd = new SimpleDateFormat("dd");
        TomorrowData data = new TomorrowData();
        data.setYear(yyyy.format(new Date(timeMillis)));
        data.setAllTime(WORKMINUTE);
        data.setYear(mm.format(new Date(timeMillis)));
        data.setYear(dd.format(new Date(timeMillis)));
        data.setCount(1);
        data.setTimeStamp(timeMillis);
        data.setYearMonthDay(ymd.format(new Date(timeMillis)));
        List<TomorrowData> tomorrowData = LitePal.where("yearMonthDay=?", data.getYearMonthDay()).find(TomorrowData.class);
        if (tomorrowData.size() > 0) {
            tomorrowData.get(0).setCount(tomorrowData.get(0).getCount() + 1);
            tomorrowData.get(0).setAllTime(tomorrowData.get(0).getAllTime() + WORKMINUTE);
            tomorrowData.get(0).save();
        } else {
            data.save();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (ClickUtil.isFastClick(1000)) {
                Toast.makeText(TextTomorrowActivity.this, "请集中精神", Toast.LENGTH_LONG).show();
                return true;
            } else {
                finish();
            }
        }
        return false;
    }

    private void setMinText(int minute) {
        if (minute < 10) {
            mTvMin.setText("0" + minute);
        } else {
            mTvMin.setText(minute + "");
        }
    }

    private void setSecText(int second) {
        if (second < 10) {
            mTvSec.setText("0" + second);
        } else {
            mTvSec.setText(second + "");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCountDownTimer.cancel();
    }
}
